/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
#include <vector>
#include <algorithm>
using namespace std;

static bool mycmp(const Interval &a, const Interval &b) {
    return a.start<b.start;
}

class Solution {
public:
    vector<Interval> merge(vector<Interval> &intervals) {
        sort(intervals.begin(), intervals.end(), mycmp);
        vector<Interval> ret;
        bool hasMerge=false;
        Interval tmp;
        for (int i=0;i<intervals.size();i++)
            if (hasMerge)
                if (tmp.end<intervals[i].start) {
                    ret.push_back(tmp);
                    tmp.start=intervals[i].start;
                    tmp.end=intervals[i].end;
                } else
                    tmp.end=max(tmp.end, intervals[i].end);
            else {
                hasMerge=true;
                tmp.start=intervals[i].start;
                tmp.end=intervals[i].end;
            }
        if (hasMerge) ret.push_back(tmp);
        return ret;
    }
};
